package com.icebergteam.warfacewidget.news.ui;

import java.util.Date;
import java.util.Locale;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.WidgetProvider;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.core.OnTaskCompleted;
import com.icebergteam.warfacewidget.core.Parser;
import com.icebergteam.warfacewidget.core.RequestTask;
import com.icebergteam.warfacewidget.core.connection.NetworkManager;
import com.icebergteam.warfacewidget.core.connection.services.RSSFeedService;
import com.icebergteam.warfacewidget.core.utils.TimeDateUtils;
import com.icebergteam.warfacewidget.core.utils.ViewUtils;
import com.icebergteam.warfacewidget.news.data.NewsParser;
import com.icebergteam.warfacewidget.R;

	public class NewsRequestTask extends RequestTask {

		public static final String className = NewsRequestTask.class.getCanonicalName();
		
		private NewsParser rssParser;
		
		public NewsRequestTask(OnTaskCompleted onTaskCompleted, Context context, Intent intent) {
			super(onTaskCompleted, context, intent);
			
			 this.rssParser = new NewsParser(this.intent.getExtras().getBoolean(Parser.KEY_INTENT_RESTORE));
		}

		
		@Override
		protected Integer doInBackground(Void... paramas) {
			try {
				this.rssParser.saveData(NetworkManager.getUri(new RSSFeedService(), false));
			} catch (Exception e) {
				LogManager.LogEA(NewsRequestTask.className, ViewUtils.getErrorMessage(e));
				return this.rssParser.getStateRequest();
			}
			return this.rssParser.getStateRequest();
		}

		@Override
		protected void onPostExecute(Integer result) {
			this.intent.putExtra(WidgetProvider.TYPE_LIST, WidgetProvider.NEWS_TYPE_LIST);
			if(!rssParser.isRestoreKey()){
				this.intent.putExtra(Parser.KEY_INTENT_RESTORE, false);
			}
			WidgetProvider.setTypeCurrentService(WidgetProvider.NEWS_TYPE_LIST);
			
			super.onPostExecute(result);
		}
		
		public static void setWidgetState(RemoteViews view, int result){
			view.setEmptyView(R.id.news_list_view, R.id.emptyTextView);
			view.setTextViewText(R.id.emptyTextView,  (result == (Parser.REQUEST_FAILED ) || result == Parser.DB_INVALID) ? AppManager.getContext().getResources().getText(R.string.errorTextView ): AppManager.getContext().getResources().getText(R.string.emptyTextView) );
				
			try {
				Date lastUpdateDate = AppManager.getDatabase().getNewsTable().getLastUpdate();
				String date = String.format(
						"%s", ((lastUpdateDate.equals(new Date(0))) ? "": TimeDateUtils.getDateByLocale(lastUpdateDate, new Locale(AppManager.getLocaleManager().getCurrentUILangId()), "dd.MM.yy" /*HH:mm*//*"dd MMM yyyy HH:mm:ss"*/)));
			
				view.setTextViewText(R.id.widget_control_panel_date_TextView, date);
					
				LogManager.LogVA(NewsRequestTask.className, result == Parser.REQUEST_COMPLETED,  result == (Parser.REQUEST_FAILED ), view);
			} catch (Exception e) {
				LogManager.LogEA(NewsRequestTask.className, ViewUtils.getErrorMessage(e));
			}
		}
	}